﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Model;
using Cn.Cellula.Biz;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Edwon.Web;
using System.Reflection;
using Cn.Edwon.Web.Validation;
using Cn.Cellula.Model.Security;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.User {
    public partial class EditProfile : PageSupport {
        IUserBiz biz = BizFactory.Get<IUserBiz>();
        String userKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            userKey = (Request.QueryString["id"] == null) ? this.Actor.ID : Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (this.Actor != null) {
                    FillForm();
                }
            }
        }
        private void FillForm() {
            try {
                VSysUserEO user = biz.Load<VSysUserEO>(userKey);
                this.lab_LoginName.Text = user.LoginName;
                this.lab_UserName.Text = user.UserName;
                this.txt_Email.Text = user.Email;
                this.txt_UserName.Text = user.UserName;
                this.txt_Birthday.Text = (user.Birthday==null)?"":Convert.ToDateTime(user.Birthday).ToString("yyyy-MM-ss");
                this.txt_Summary.Text = user.Summary;
                this.txt_UserCertId.Text = user.CertId;
                this.ddl_Gender.SelectedValue = user.Gender+"";
                this.ddl_UserCertType.SelectedValue = user.CertTypeKey;
                this.txt_RealName.Text = user.RealName;
                this.txt_Mobile.Text = user.Mobile;
                this.lab_UserNo.Text = user.UserNo;
                this.btn_save.Enabled = PowerBiz.UserEdit(this.Actor, user).Editable;
                //已审核的用户，email不可更改
                this.txt_Email.Enabled = PowerBiz.UserEdit(this.Actor, user).Editable;

            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "装载用户数据时错误");   
            }
        }
        private void InitForm() {
            ClientBiz.BindCodeList(ref this.ddl_UserCertType, CodeTypeDef.UserCert.Key, true, null);
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            if (this.Actor == null) return;
            Boolean bk = QuickValidator.Execute(Request,this.txt_Birthday.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            try {
                TSysUserEO user = biz.Load<TSysUserEO>(userKey);
                user.CertId = this.txt_UserCertId.Text;
                user.CertTypeKey = this.ddl_UserCertType.SelectedValue;
                if (user.Email != this.txt_Email.Text && biz.IfEmailExists(this.txt_Email.Text,user.UserKey)) {
                    SiteInfo.Send(Page, TxtDef.ERROR, "Email(" + this.txt_Email.Text + ")已存在，请重新输入");
                    return;
                }
                if (user.UserName != this.txt_UserName.Text && biz.IfUserNameExists(this.txt_UserName.Text, user.UserKey)) {
                    SiteInfo.Send(Page, TxtDef.ERROR, "用户名(" + this.txt_UserName.Text + ")已存在，请重新输入");
                    return;
                } 
                user.Email = this.txt_Email.Text;
                user.Mobile = this.txt_Mobile.Text;
                user.Summary = this.txt_Summary.Text;
                user.UserName = this.txt_UserName.Text;
                user.RealName = this.txt_RealName.Text;
                user.Birthday = Convert.ToDateTime(this.txt_Birthday.Text);
                try {
                    user.Birthday = Convert.ToDateTime(this.txt_Birthday.Text);
                    user.Gender = Convert.ToInt32(this.ddl_Gender.SelectedValue);
                } catch (Exception ex) {
                    log.Error(ex.Message);
                    SiteInfo.Send(Page, TxtDef.ERROR, "更新时出现错误");
                    return;
                }
                biz.Update<TSysUserEO>(user);
                BizFactory.Get<ILogBiz>().LogUpdate("基本资料", user.LoginName, this.Actor, Request);
                SiteInfo.SendSucceed(Page, "");
            } catch (Exception ex) {
                log.Error("更新用户资料时出现错误:" + ex.Message);
                SiteInfo.Send(Page, TxtDef.FAILED, "更新用户资料时出现错误");
            }
        }

    }
}
